<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TeamController
 *
 * @author Poh Nean
 */
class TeamsController extends PsController
{

	public $layout = 'manage';

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'actions' => array('index', 'view'),
						'roles' => array('ManageAgency.Team.View'),
					),
					array('allow',
						'actions' => array('create', 'update', 'delete'),
						'roles' => array('ManageAgency.Team.Manage'),
					),
						), parent::accessRules());
	}

	public function actionIndex()
	{
		$agencyTeam = new AgencyTeam('Search');
		if (isset($_GET['AgencyTeam'])) {
			$agencyTeam->unsetAttributes();
			$agencyTeam->attributes = $_GET['AgencyTeam'];
		}

		// Dataprovider
		$criteria = new CDbCriteria;
//		$criteria->addInCondition('t.agency_id', Yii::app()->user->getCurrentAgencyIds());
		$criteria->addInCondition('t.agency_id', Yii::app()->user->getSelectedAgency()->getChildAgencyIds());
		
		$criteria->compare('t.name', $agencyTeam->name, true);

		$dataProvider = new CActiveDataProvider('AgencyTeam', array(
					'criteria' => $criteria,
					'sort' => array(
						'defaultOrder' => 't.name ASC'
					),
					'pagination' => array(
						'pageSize' => 50,
					)
				));

		$data = array();
		$data['agencyTeam'] = $agencyTeam;
		$data['dataProvider'] = $dataProvider;
		
		$this->render('index', $data);
	}

	public function actionCreate()
	{
		$agencyTeam = new AgencyTeam;

		if (isset($_POST['AgencyTeam'])) {
			$agencyTeam->attributes = $_POST['AgencyTeam'];
			if ($agencyTeam->agency_id === null) {
				$agencyTeam->agency_id = Yii::app()->user->getSelectedAgencyId();
			}
			
			$isValid = $agencyTeam->validate();

			if ($isValid) {
				PsSystemMessage::add('Team "' . $agencyTeam->name . '" added', 'success');
				$agencyTeam->save(false);
				$this->redirect(array('teams/index'));
			}
		}

		$data = array();
		$data['agencyTeam'] = $agencyTeam;

		$this->render('create', $data);
	}

	public function actionUpdate($id)
	{
		$agencyTeam = $this->loadAgencyTeam($id);

		if (isset($_POST['AgencyTeam'])) {
			$agencyTeam->attributes = $_POST['AgencyTeam'];
			$isValid = $agencyTeam->validate();

			if ($isValid) {
				PsSystemMessage::add('Team "' . $agencyTeam->name . '" updated', 'success');
				$agencyTeam->save(false);
				$this->redirect(array('teams/index'));
			}
		}

		$data = array();
		$data['agencyTeam'] = $agencyTeam;

		$this->render('update', $data);
	}

	public function actionDelete($id)
	{
		// check if any users are in this team
		$usersCount = User::model()->ofCurrentAgency()->countByAttributes(array(
			'agency_team_id' => $id
				));

		if ($usersCount > 0) {
			PsSystemMessage::add('You cannot delete a team with members.');
		} else {
			$agencyTeam = $this->loadAgencyTeam($id);
			$agencyTeamName = $agencyTeam->name;

			if ($agencyTeam->delete()) {
				PsSystemMessage::add('Team: ' . $agencyTeamName . ' deleted.');
				$this->redirect(array('teams/index'));
			}
		}
	}

	public function loadAgencyTeam($id)
	{
		$model = AgencyTeam::model()->ofCurrentAgency()->findByPk($id);

		if ($model == null) {
			$this->redirect(array('teams/index'));
		}

		return $model;
	}

}

?>
